package com.atguigu.springcloud.httputil;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientPostExample {

    private static final int CONNECT_TIME_OUT = 3000;

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "http://127.0.0.1:8080/api/testApiService/getTwo";
        HttpPost httpPost = new HttpPost(url);

        JSONObject json = new JSONObject();
        json.put("customerNo","张三");
//        String jsonInput = "{\"customerNo\":\"\", \"key2\":\"value2\"}";
        StringEntity entity = new StringEntity(json.toString());
//        RequestConfig config = RequestConfig.custom().
//                setConnectionRequestTimeout(CONNECT_TIME_OUT).setSocketTimeout()
//                build();


//        httpPost.setConfig(config);
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            System.out.println("POST响应码: " + response.getStatusLine().getStatusCode());
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                System.out.println(EntityUtils.toString(responseEntity));
            }
        } finally {
            response.close();
        }
    }
}